package be.phl.mycinema.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import be.phl.mycinema.DetailActivity;
import be.phl.mycinema.R;
import be.phl.mycinema.Utils;
import be.phl.mycinema.classes.DownloadImageTask;
import be.phl.mycinema.classes.Film;
import be.phl.mycinema.classes.MemoryCache;

public class MyAccountAdapter extends BaseAdapter implements OnClickListener {

	private final static String TAG = "ACCOUNT ADAPTER";
	private ArrayList<Film> films;
	private LayoutInflater inflater;
	private Context context;
	private MemoryCache memoryCache;
		
	public MyAccountAdapter(Context context,ArrayList<Film> films,String id) {
		inflater = LayoutInflater.from(context);
		this.films = films;
		this.context = context;
		
		if (memoryCache == null) {
			 memoryCache = MemoryCache.createMemoryCache(context);
		}
	}
	
	public int getCount() {
		return films.size();
	}

	public Object getItem(int position) {
		return films.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder;
		
		convertView = inflater.inflate(R.layout.account_row, null);
		holder = new ViewHolder();
			 
		holder.Titel = (TextView) convertView.findViewById(R.id.account_titel);
		holder.looptijd = (TextView) convertView.findViewById(R.id.account_looptijd);
		holder.play = (ImageButton) convertView.findViewById(R.id.account_play);
		holder.Afbeelding = (ImageView) convertView.findViewById(R.id.account_image);
		 
		holder.Titel.setText(films.get(position).getTitel());
		holder.looptijd.setText("Looptijd " + GetTime(films.get(position).getLooptijd()));
		holder.play.setTag(films.get(position).getStream_url());
		holder.play.setOnClickListener(this);
		
		Bitmap t = memoryCache.getBitmapFromMemCache(films.get(position).getFilm_ID());

		if (t == null) {
			holder.Afbeelding.setImageResource(R.drawable.empty_photo);
			new DownloadImageTask(films.get(position).getAfbeelding(), films.get(position).getFilm_ID(), holder.Afbeelding, memoryCache).execute("");
		} else {
			holder.Afbeelding.setImageBitmap(t);
		}
		 
		 convertView.setClickable(true);
		 convertView.setFocusable(true);
		 convertView.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent(context, DetailActivity.class);
				i.putExtra("FILM", films.get(position));
				context.startActivity(i);
			}
		});
		 
		return convertView;	
	}
	
	private String GetTime(String t) {
		int hours = Integer.valueOf(t) / 60;
		int minutes = Integer.valueOf(t) % 60;

		return String.format("%d:%02d", hours, minutes);
	}
	
	private static class ViewHolder {
		TextView Titel;
		TextView looptijd;
		ImageButton play;
		ImageView Afbeelding;
	}
	
	public void onClick(View v) {		

		Log.d(TAG, "Video afspelen");
		Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.parse(String.format("http://" + Utils.IP_ADDRESS_NO_PORT + "%s", v.getTag().toString())), "video/mp4");
        context.startActivity(i);
    
	}

}
